﻿Imports System.Data
Imports System.Data.OleDb
Imports validaciones.metodos

Public Class Adopciones

    Public validar As New validaciones.metodos
    Public conexion As New OleDb.OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0; Data Source=nuevozoo.accdb")

    Private Sub BotonesHabilitados(ByVal modo As Boolean)
        b_añadirAdop.Enabled = modo
        b_eliminarAdop.Enabled = modo
        b_modificarAdop.Enabled = modo
        b_nuevaAdop.Enabled = modo
    End Sub

    Private Sub cargarAdopciones()

        'ADOPCIONES---------------------------------------------------------------------

        'para adopciones
        Dim adaptador As New OleDbDataAdapter("select * from adopciones order by cod_animal", conexion)
        Dim miDataset As New DataSet

        'limpiar DataBlindings para que no de error 
        text_codan.DataBindings.Clear()
        text_carac.DataBindings.Clear()
        text_adop.DataBindings.Clear()

        'rellenar datagriview
        adaptador.Fill(miDataset, "adopciones")
        DataGridViewAdop.DataSource = miDataset
        DataGridViewAdop.DataMember = "adopciones"

        'cambiar tamaño de las columnas
        DataGridViewAdop.Columns("caracteristicas").Width = 140
     
        'ocultar columna de codigo
        DataGridViewAdop.Columns("cod_animal").Visible = False

        'rellenar textbox
        text_codan.DataBindings.Add("text", miDataset, "adopciones.cod_animal")
        text_carac.DataBindings.Add("text", miDataset, "adopciones.caracteristicas")
        text_adop.DataBindings.Add("text", miDataset, "adopciones.adoptado")
    End Sub

    Private Sub VolverToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VolverToolStripMenuItem.Click
        GestionGeneral.Show()
        Me.Close()
    End Sub

    Private Sub Adopciones_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarAdopciones()
    End Sub

    Private Sub b_nuevoemp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_nuevaAdop.Click
        'bloqueo el datagridview para que no se pueda modificar a pincho
        DataGridViewAdop.Enabled = False

        'desbloqueo el boton añadir que viene bloqueado por defecto dejando el resto bloqueados
        BotonesHabilitados(False)
        b_añadirAdop.Enabled = True

        text_codan.Text = "Se autorrellena"
        text_carac.Text = ""
        text_adop.Text = "False"
        text_adop.Enabled = False

    End Sub

    Private Sub b_añadirAdop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_añadirAdop.Click
        'sacar ultimo codigo de adopciones usado
        Dim cmd As New OleDbCommand("select max(cod_animal) from adopciones", conexion)
        conexion.Open()
        Dim a As Object = cmd.ExecuteScalar
        Dim codMaxUsado As String = a.ToString
        Dim codNuevo As Integer = codMaxUsado + 1 'codigo del nuevo registro

        'introducir nuevo registro
        If text_carac.Text <> "" Then
            Dim myCommand As OleDbCommand
            myCommand = New OleDbCommand("INSERT INTO adopciones VALUES(" & codNuevo & ",'" & text_carac.Text & "',false )", conexion)
            myCommand.ExecuteNonQuery()
        Else
            MsgBox("Los campos no pueden estar vacíos", , "Error")
        End If

        conexion.Close()
        cargarAdopciones()

        'deshabilito el boton añadir nuevamente y habilito los datagridview y demas botones
        BotonesHabilitados(True)
        b_añadirAdop.Enabled = False
        text_adop.Enabled = True
        DataGridViewAdop.Enabled = True
    End Sub


    Private Sub b_modificarAdop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_modificarAdop.Click
        'modificar adopcion
        Dim res As Integer

        If text_carac.Text <> "" Then

            res = MsgBox("¿Está seguro de modificar el animal?", MsgBoxStyle.YesNo, "Aviso")
            If res = vbYes Then
                conexion.Open()
                Dim myCommand As OleDbCommand
                myCommand = New OleDbCommand("update adopciones set caracteristicas ='" & text_carac.Text & "', adoptado = " & text_adop.Text & " where cod_animal = " & text_codan.Text, conexion)
                myCommand.ExecuteNonQuery()
                conexion.Close()
            End If

        Else
            MsgBox("Los campos no pueden estar vacíos", , "Error")
        End If
        cargarAdopciones()
    End Sub

    Private Sub b_eliminarAdop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_eliminarAdop.Click
        'borrar adopcion seleccionado
        Dim res As Integer
        res = MsgBox("¿Está seguro de borrar el animal?", MsgBoxStyle.YesNo, "Aviso")
        If res = vbYes Then
            conexion.Open()
            Dim myCommand As OleDbCommand
            myCommand = New OleDbCommand("delete from adopciones where cod_animal = " & text_codan.Text, conexion)
            myCommand.ExecuteNonQuery()
            conexion.Close()

            cargarAdopciones()
        End If
    End Sub
End Class